export default {

	//设置cookie
	Cookieset(key, value, delay) {
		//默认cookie为七天之后过期 3s 4m 5h 7d 秒 分 时 天
		if (delay == "undefined") delay = "7d";
		delay = delay.toLowerCase();

		var expireDate = new Date();

		var num = parseInt(delay);
		if (delay.indexOf("d") !== -1) {
			expireDate.setDate(expireDate.getDate() + num);
		} else if (delay.indexOf("h") !== -1) {
			expireDate.setHours(expireDate.getHours() + num);
		} else if (delay.indexOf("m") !== -1) {
			expireDate.setMinutes(expireDate.getMinutes() + num);
		} else if (delay.indexOf("s") !== -1) {
			expireDate.setSeconds(expireDate.getSeconds() + num);
		} else {
			expireDate.setDate(expireDate.getDate() + num);
		}
		if (typeof value == "object") {
			value = JSON.stringify(value);
		}

		document.cookie = key + "=" + value + ";expires=" + expireDate.toGMTString();
	},

	//得到cookie 如果不存在 返回 undefined
	Cookieget(name) {
		var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		if (arr = document.cookie.match(reg)) {
			return (arr[2]);
		} else {
			return null;
		}
	},
	//删除cookie
	Cookiedel(name) {
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval = this.Cookieget(name);
		if (cval != null)
			document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
	},
	CookiedecodeJson(value) {
		//数组转成的对象字符串
		var regAryStr = /^\[[\s|\S]*\]$/;
		//对象转成的对象字符串
		var regObjStr = /^\{([\"\s|\S]+\"\:\"[\s|\S]*)+\"\}$/;
		if (regAryStr.test(value)) {
			return eval("(" + value + ")");
		}
		if (regObjStr.test(value)) {
			return JSON.parse(value);
		}
		return value;
	}
}
